﻿/// <reference path="utils.js" />
'use strict';

define([
    'common/models/customer.model',
    'backbone.paginator',
], function (Model) {
    var Collection = Backbone.PageableCollection.extend({
        defaults: {
            model: Model,
        },

        fetch: function (options) {
            var that = this;

            if (!options) options = {};

            App.billingService.getCatalog(
                { name: 'Customer', },
                _.extend({}, options, {
                    success: function (resp) {
                        try {
                            if (resp.Success == true) {
                                if (resp.Data) {
                                    var newColl = [];

                                    _.each(resp.Data, function (item, i) {
                                        newColl.push(new Model({
                                            active: item.Active,
                                            address: item.Address,
                                            city: item.City,
                                            code: item.Code,
                                            contact: item.Contact,
                                            cuit: item.Cuit,
                                            description: item.Description,
                                            email: item.Email,
                                            id: item.Id,
                                            name: item.Name,
                                            telephone: item.Telephone,
                                        }));
                                    });

                                    that.set(newColl);

                                    if (options.success)
                                        options.success(that);
                                }
                                else {
                                    console.debug('data null');

                                    if (options.error)
                                        options.error('data null');
                                }
                            }
                            else {
                                console.debug(resp.Message);

                                if (options.error)
                                    options.error(resp.Message);
                            }
                        }
                        catch (Error) {
                            if (options.async) {
                                console.error(Error);

                                if (options.error)
                                    options.error('Exception');
                            }
                            else {
                                throw Error;
                            }
                        }
                    },
                    error: function () {
                        console.debug('error');

                        if (options.error)
                            options.error('error');
                    },
                    //async: false,
                })
            );

            return this;
        },
    });

    return Collection;
});